<?php

namespace App\Http\Controllers\Api;

use App\Order;
use App\Socialite;
use App\SubjectPower;
use App\TalentsSystemOrder;
use EasyWeChat\Factory;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class PayController extends Controller
{
    protected $payType;
    protected $body = '天南山文化';
    protected $subject = '天南山文化';
    protected $notifyUrl;

    public function order($type, $id)
    {
        $this->payType = $type;
        $this->notifyUrl = url('/api/notify', [$this->payType . '-' . $id]);
        $order = Order::find($id);
        if (!$order) {
            return response(['message' => '订单不存在'], 404);
        }
        return $this->_uniformOrder($order);
    }

    protected function _uniformOrder(Order $order)
    {
        if ($this->payType == 'wxpay') {
            return $this->_uniformOrderForOfficialAccountPay($order);
        } else {
            return response(['message' => '支付方式不存在'], 400);
        }
    }

    protected function _uniformOrderForOfficialAccountPay(Order $order)
    {
        $payment = Factory::payment(config('wechat.payment.default'));
        $openid = Socialite::where('user_id', Auth::id())->where('driver', 'wechat')->value('open_id');
        if (!$openid) {
            return response(['尚未绑定微信'], 401);
        }
        $attribute = [
            'body' => $this->body,
            'detail' => $this->subject,
            'out_trade_no' => $order->order_sn,
            'total_fee' => $order->order_amount * 100,
            'notify_url' => $this->notifyUrl,
            'trade_type' => 'JSAPI',
            'openid' => $openid,
        ];
        $result = $payment->order->unify($attribute);
        if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
            return $payment->jssdk->bridgeConfig($result['prepay_id']);
        }
        return response(['message' => '支付信息核对中,请稍后查看支付结果'], 400);
    }

    /**
     * @param $type
     * @param $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 购买评估支付
     */
    public function Subject($type, $id)
    {
        $this->payType = $type;
        $this->notifyUrl = url('/api/notify/subject', [$this->payType . '-' . $id]);
        $order = TalentsSystemOrder::find($id);
        if (!$order) {
            return response(['message' => '订单不存在'], 404);
        }
        if ($type == 'wxpay') {
            return $this->_subjectwxpay($order);
        } else {
            return response(['message' => '支付方式不存在'], 400);
        }
    }

    /**
     * @param $order
     * @return array|\Illuminate\Contracts\Routing\ResponseFactory|string|\Symfony\Component\HttpFoundation\Response
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    protected function _subjectwxpay($order)
    {
        $payment = Factory::payment(config('wechat.payment.default'));
        $openid = Socialite::where('user_id', Auth::id())->where('driver', 'wechat')->value('open_id');
        if (!$openid) {
            return response(['尚未绑定微信'], 401);
        }
        $attribute = [
            'body' => $this->body,
            'detail' => $this->subject,
            'out_trade_no' => $order->order_number,
            'total_fee' => $order->price * 100,
            'notify_url' => $this->notifyUrl,
            'trade_type' => 'JSAPI',
            'openid' => $openid,
        ];
        $result = $payment->order->unify($attribute);
        if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
            return $payment->jssdk->bridgeConfig($result['prepay_id']);
        }
        return response(['message' => '支付信息核对中,请稍后查看支付结果'], 400);
    }

    /**
     * @param $type
     * @param $id
     * @return array|\Illuminate\Contracts\Routing\ResponseFactory|string|\Symfony\Component\HttpFoundation\Response
     */
    public function teamPay($type, $id)
    {
        $this->payType = $type;
        $this->notifyUrl = url('/api/notify/team', [$this->payType . '-' . $id]);
        $order = TalentsSystemOrder::find($id);
        if (!$order) {
            return response(['message' => '订单不存在'], 404);
        }
        if ($type == 'wxpay') {
            return $this->_teamwxpay($order);
        } else {
            return response(['message' => '支付方式不存在'], 400);
        }
    }

    /**
     * @param $order
     * @return array|\Illuminate\Contracts\Routing\ResponseFactory|string|\Symfony\Component\HttpFoundation\Response
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    protected function _teamwxpay($order)
    {
        $payment = Factory::payment(config('wechat.payment.default'));
        $openid = Socialite::where('user_id', Auth::id())->where('driver', 'wechat')->value('open_id');
        if (!$openid) {
            return response(['尚未绑定微信'], 401);
        }
        $attribute = [
            'body' => $this->body,
            'detail' => $this->subject,
            'out_trade_no' => $order->order_number,
            'total_fee' => $order->price * 100,
            'notify_url' => $this->notifyUrl,
            'trade_type' => 'JSAPI',
            'openid' => $openid,
        ];
        $result = $payment->order->unify($attribute);
        if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
            return $payment->jssdk->bridgeConfig($result['prepay_id']);
        }
        return response(['message' => '支付信息核对中,请稍后查看支付结果'], 400);
    }
}
